﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using AtletIS.Shared.Entities;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.WpfControls
{
    public partial class LiveResults : UserControl
    {
        private int PAGE_SIZE = Properties.Settings.Default.PAGE_SIZE;
        private int pageIndex = 0;
        private int pages = 1;

        private System.Windows.Forms.Timer tmrRefresh;
        private DisciplineListEntity discipline;
        private DataTable dtPerformances;
        private DataRow[] aPerformancesRanked;
        private DataRow[] aPerformancesUnranked;

        // Constructor
        public LiveResults(DisciplineListEntity discipline)
        {
            InitializeComponent();
            this.discipline = discipline;

            cbTitle.title1 = discipline.Name;
            cbTitle.title2 = "Live Results";
        }

        // Loaded
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.dtPerformances = DatabaseHelper.GetPerformancesByDisciplineListId(discipline.ID); 

            this.aPerformancesRanked = dtPerformances.Select("Rank > 0 OR (Rank = 0 AND Code <> 0)", "Code, Rank");
            this.aPerformancesUnranked = dtPerformances.Select("Rank = 0 AND Code = 0", "Code, Rank");

            // Pages
            if (this.aPerformancesRanked.Length % PAGE_SIZE == 0)
                this.pages = this.aPerformancesRanked.Length / PAGE_SIZE;
            else
                this.pages = this.aPerformancesRanked.Length / PAGE_SIZE + 1;

            // Timer
            this.tmrRefresh = new System.Windows.Forms.Timer();
            this.tmrRefresh.Interval = 1000;
            this.tmrRefresh.Tick += new System.EventHandler(this.tmrRefresh_Tick);
            this.tmrRefresh.Enabled = true;
            this.FillListView();
        }

        // Timer Tick
        private void tmrRefresh_Tick(object sender, EventArgs e)
        {
            this.FillListView();
        }

        // Fill ListView
        private void FillListView()
        {
            string sResult, sPbSb;
            int prevRank, nRank;

            this.lbLiveResults.Items.Clear();

            // Print
            prevRank = 0;
            for (int i = this.pageIndex * PAGE_SIZE; i < this.pageIndex * PAGE_SIZE + PAGE_SIZE; i++)
            {
                if (i < this.aPerformancesRanked.Length)
                {
                    if (Convert.ToInt32(aPerformancesRanked[i]["Rank"]) == prevRank)
                        nRank = 0;
                    else
                        nRank = Convert.ToInt32(aPerformancesRanked[i]["Rank"]);

                    sResult = Helpers.PrintHelper.PrintResultWithCode(Convert.ToInt32(aPerformancesRanked[i]["Code"]), aPerformancesRanked[i]["Result"].ToString(), discipline);
                    sPbSb = Helpers.PrintHelper.PrintPbSb(discipline.DisciplineCode,
                        aPerformancesRanked[i]["ResultQuality"].ToString(),
                        Convert.ToInt32(aPerformancesRanked[i]["Code"]), 
                        Convert.ToInt32(aPerformancesRanked[i]["Result"]),
                        Convert.ToInt32(aPerformancesRanked[i]["PB"]),
                        Convert.ToInt32(aPerformancesRanked[i]["SB"]));

                    SixAttemptsBox grb = new SixAttemptsBox(nRank,
                        Convert.ToInt32(aPerformancesRanked[i]["StartNumber"]),
                        aPerformancesRanked[i]["CountryId"].ToString(),
                        aPerformancesRanked[i]["Surname"].ToString().ToUpper() + " " + aPerformancesRanked[i]["FirstName"].ToString(),
                        sResult,
                        Convert.ToInt32(aPerformancesRanked[i]["ID"]),
                        discipline);

                    lbLiveResults.Items.Add(grb);
                    prevRank = Convert.ToInt32(aPerformancesRanked[i]["Rank"]);
                }
            }

            this.footer.txtPage.Text = (this.pageIndex + 1).ToString() + " / " + this.pages.ToString();
        }

        #region Prev / Next

        private void UserControl_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (this.pages > 1)
                {
                    if (e.Key == Key.Right)
                    {
                        this.pageIndex += 1;

                        if (this.pageIndex >= this.pages)
                            this.pageIndex = this.pages - 1;

                        this.FillListView();
                    }

                    if (e.Key == Key.Left)
                    {
                        this.pageIndex -= 1;

                        if (this.pageIndex < 0)
                            this.pageIndex = 0;

                        this.FillListView();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Live Výsledky", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.pageIndex -= 1;

                if (this.pageIndex < 0)
                    this.pageIndex = 0;

                this.FillListView();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Live Výsledky", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.pageIndex += 1;

                if (this.pageIndex >= this.pages)
                    this.pageIndex = this.pages - 1;

                this.FillListView();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Live Výsledky", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        #endregion

    }
}
